#coding=utf-8
from django.db import models
from django.forms import ModelForm
from django import forms
from django.shortcuts import render_to_response
from django.template import loader, Context
from django.http import HttpResponseRedirect,HttpResponse
from django.http import Http404
from models import call
from zhaokelai.users.models import users
from zhaokelai.common import skinFolder,checkLogin
from django.core.paginator import Paginator,InvalidPage,EmptyPage,PageNotAnInteger

@checkLogin 
def call_list(request,page=1):
    after_range_num = 5
    bevor_range_num = 4
    info = call.objects.order_by('-id').all()
    paginator = Paginator(info,8)
    if page:
        page=int(page)
    try:
        pageObj=paginator.page(page)
    except(EmptyPage,InvalidPage,PageNotAnInteger):
        pageObj = paginator.page(1)
        
    if page>= after_range_num:
        page_range = paginator.page_range[page-after_range_num:page+bevor_range_num]
    else:
        page_range = paginator.page_range[0:int(page)+bevor_range_num]
    return render_to_response(skinFolder(request)+'marketing/call_list.html',locals())

@checkLogin 
def call_detail(request,id):
    if id :
        id=int(id)
        obj = call.objects.get(pk=id)
        return render_to_response(skinFolder(request)+'marketing/call_detail.html',{'obj': obj})
    else:
        raise Http404()

class callForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(callForm, self).__init__(*args, **kwargs)
        for k in self.fields:
            try:
                self.fields[k].required = False
            except:
                pass
    class Meta:
        model = call
        
@checkLogin 
def income_add(request):
    if request.method == 'POST': 
        form = callForm(request.POST)
        if form.is_valid(): 
            newnn=form.save()
            return HttpResponseRedirect('/marketing/call/') # Redirect after POST
    else:
        form = callForm() # An unbound form

    return render_to_response(skinFolder(request)+'marketing/call_edit.html',{
        'form': form,
        'action':'添加',
    })        
        
@checkLogin 
def call_modify(request,id):
    if id :
        id=int(id)
        obj = call.objects.get(pk=id)
        if request.method == 'POST': 
            form = callForm(request.POST, instance=obj)
            sobj=form.save()
            return HttpResponseRedirect('/marketing/call/') # Redirect after POST
        else:
            form = callForm(instance=obj)
            return render_to_response(skinFolder(request)+'marketing/call_edit.html',{'form': form,'action':'修改',})
    else:
        raise Http404()
    
@checkLogin 
def call_delete(request,id=None):
    if id :
        id=int(id)
        objs = call.objects.get(pk=id)
        objs.delete()
    elif request.method == 'POST':       
        ids=request.POST.getlist("tid")
        if ids and len(ids)>0:
            idstring = ','.join(ids)
            call.objects.extra(where=['id IN ('+idstring+')']).delete()
    else:
        pass
    return HttpResponseRedirect('/marketing/call/')    
